import 'package:enjoy_plus/models/home_noticelist_model.dart';
import 'package:enjoy_plus/pages/home/components/home_list.dart';
import 'package:enjoy_plus/pages/home/components/home_nav.dart';
import 'package:enjoy_plus/utils/http.dart';
import 'package:enjoy_plus/utils/logger.dart';
import 'package:enjoy_plus/utils/toast.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<NotifyItem> notifyList = [];

  void getNotifyList() async {
    try {
      final response = await http.get<List<NotifyItem>>(
        '/announcement',
        fromJson: (data) {
          final List<dynamic> list = data as List;
          return list.map((item) => NotifyItem.fromJson(item)).toList();
        },
      );

      if (response.code == 10000) {
        setState(() {
          notifyList = response.data;
        });
        // logger.d('notifyList: ${notifyList.toString()}');
        ToastUtil.showSuccess('获取数据成功');
      } else {
        ToastUtil.showError(response.message);
      }
    } catch (e) {
      ToastUtil.showError('获取数据失败');
      logger.e(e);
    }
  }

  @override
  void initState() {
    super.initState();
    getNotifyList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(40, 85, 145, 175),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        scrollDirection: Axis.vertical,
        padding: const EdgeInsets.all(10),
        children: [
          HomeNav(),
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.asset('assets/images/banner@2x.jpg'),
          ),
          HomeList(notifyList: notifyList),
        ],
      ),
    );
  }
}
